package com.shijiwei.lifthelper.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.shijiwei.lifthelper.R;
import com.zbar.lib.CaptureActivity;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by sjw on 2015/12/19.
 */
public class CodeResultActivity extends AppCompatActivity implements View.OnClickListener {
    @ViewInject(R.id.tv_back)
    private TextView back;
    @ViewInject(R.id.tv_content)
    private TextView contents;
    @ViewInject(R.id.tv_search)
    private TextView search;



    private String content;
    public static final int REQUST_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ccode);
        x.view().inject(this);
        startActivityForResult(new Intent(this, CaptureActivity.class), REQUST_CODE);

        addListener();
    }

    private void addListener() {

        back.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUST_CODE){
            if (resultCode == RESULT_OK){
                Toast.makeText(this,data.getStringExtra("result"),Toast.LENGTH_SHORT).show();
                if (data.getStringExtra("result").equals("cancle")){
                    finish();
                }
                content = data.getStringExtra("result");
                contents.setText(content);

                if (content.startsWith("http://")){
                    search.setVisibility(View.VISIBLE);
                }


            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_search:
                Intent intent = new Intent(this,WebActivity.class);
                intent.putExtra("url",contents.getText().toString());
                startActivity(intent);
                break;
        }
    }
}
